//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
#include "LigandNearResidues.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
#define LENGTHTEXT 500000
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
LigandNearResidues::LigandNearResidues () {
  
  this->ligandName   = new String ();
  this->ligandNumber = 0;
  this->ligandSize   = 0;
  this->nearResidues = new TListE <String> ();
  this->toString     = new String ();
    
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
LigandNearResidues::LigandNearResidues (const LigandNearResidues& ligandNearResidues) {
  
  this->ligandName   = new String (ligandNearResidues.ligandName);
  this->ligandNumber = ligandNearResidues.ligandNumber;
  this->ligandSize   = ligandNearResidues.ligandSize;
  this->nearResidues = new TListE <String> (ligandNearResidues.nearResidues);
  this->toString     = new String ();
    
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
LigandNearResidues::LigandNearResidues (const LigandNearResidues* ligandNearResidues) {
  
  this->ligandName   = new String (ligandNearResidues->ligandName);
  this->ligandNumber = ligandNearResidues->ligandNumber;
  this->ligandSize   = ligandNearResidues->ligandSize;
  this->nearResidues = new TListE <String> (ligandNearResidues->nearResidues);
  this->toString     = new String ();
    
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
LigandNearResidues::~LigandNearResidues () {
  
  if (this->ligandName)   delete this->ligandName;
  if (this->nearResidues) delete this->nearResidues;
  if (this->toString)     delete this->toString;
    
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
void LigandNearResidues::LigandName (String* ligandName) {
  
  if (ligandName) this->ligandName->In(ligandName);
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
void LigandNearResidues::LigandNumber (int ligandNumber) {
  
  this->ligandNumber = ligandNumber;
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
void LigandNearResidues::LigandSize (int ligandSize) {
  
  this->ligandSize = ligandSize;
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
void LigandNearResidues::NearResidues (TListE <String>* nearResidues) {
  
  if (nearResidues) *(this->nearResidues) = *(nearResidues);
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
String* LigandNearResidues::LigandName (void) {
  
  return this->ligandName;
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
int LigandNearResidues::LigandNumber (void) {
  
  return this->ligandNumber;
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
int LigandNearResidues::LigandSize (void) {
  
  return this->ligandSize;
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
TListE <String>* LigandNearResidues::NearResidues (void) {
  
  return this->nearResidues;
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
bool LigandNearResidues::ReadFromString (String* data) {
  
  if (!data || !data->Length()) return false;
  
  this->CleanData();
  
  char   tmpData [data->Length() + 1];
  char*  parts = NULL;
  
  strcpy(tmpData, data->Out());
  tmpData[data->Length()] = '\0';
  
  parts = strtok(tmpData, "-;\n");
  if (parts) this->ligandName->In(parts);
  else return false;
  
  parts = strtok(NULL, "-;\n");
  if (parts) this->ligandNumber = atoi(parts);
  else return false;
    
  parts = strtok(NULL, ",-;\n");
  
  while (parts) {
    this->nearResidues->Add(new String (parts));
    parts = strtok(NULL, ",-;\n");
  }
  
  return true;
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
bool LigandNearResidues::ReadFromString (char* data) {
  
  if (!data || !strlen(data)) return false;
  
  String dataChar;
  dataChar.In(data);
  
  return this->ReadFromString(&dataChar);    

}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
String* LigandNearResidues::ToString (void) {
  
  this->toString->In("");
  
  this->toString->Add(this->ligandName);
  
  this->toString->Add("-");
  this->toString->Add(this->ligandNumber);
  this->toString->Add(";");
  
  this->nearResidues->SetInitial();
  while (this->nearResidues->SetNext()) {
    this->toString->Add(this->nearResidues->GetCurrent()->Out());
    this->toString->Add(",");
  }
  
  return this->toString;
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
void LigandNearResidues::CleanData (void) {
  
  this->ligandName->In("");
  this->ligandNumber = 0;
  this->ligandSize   = 0;
  this->nearResidues->Clear();
  this->toString->In("");
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
LigandNearResidues LigandNearResidues::operator= (const LigandNearResidues& bindingInfo) {
  
  this->ligandName->In(bindingInfo.ligandName);
  this->ligandNumber    = bindingInfo.ligandNumber;
  this->ligandSize      = bindingInfo.ligandSize;
  *(this->nearResidues) = *(bindingInfo.nearResidues);
  
  return *this;
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
